import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qr_code_app/common/constant/color_config.dart';
import 'package:todo_flutter/todo_flutter.dart';

class QrCodeCommonInput extends BaseStatefulWidget {
  final EdgeInsetsGeometry? padding;
  final String placeholder;
  final double? placeholderFontSize;
  final Color? placeholderColor;
  final double? fontSize;
  final Color? color;
  final BoxDecoration? decoration;
  final int? maxLength;
  final int? maxLines;
  final bool autofocus;
  final Widget? prefix;
  final Widget? suffix;
  final bool obscureText;
  final bool readOnly;
  final TextAlign textAlign;
  final FocusNode? focusNode;
  final String obscuringCharacter;
  final TextEditingController? controller;
  final ValueChanged<String>? onTextChange;
  final TextInputAction textInputAction;
  final TextInputType? keyboardType;
  final ValueChanged<String>? onSubmitted;
  final List<TextInputFormatter>? inputFormatters;
  final OverlayVisibilityMode prefixMode;
  final OverlayVisibilityMode suffixMode;
  final bool enableInteractiveSelection;

  const QrCodeCommonInput({
    Key? key,
    this.controller,
    this.padding,
    this.placeholder = '请输入内容',
    this.placeholderFontSize,
    this.placeholderColor,
    this.fontSize,
    this.color,
    this.decoration,
    this.maxLength,
    this.maxLines,
    this.autofocus = false,
    this.obscureText = false,
    this.readOnly = false,
    this.prefix,
    this.suffix,
    this.inputFormatters,
    this.obscuringCharacter = '•',
    this.onSubmitted,
    this.focusNode,
    this.onTextChange,
    this.textAlign = TextAlign.start,
    this.prefixMode = OverlayVisibilityMode.always,
    this.suffixMode = OverlayVisibilityMode.always,
    this.textInputAction = TextInputAction.done,
    this.keyboardType = TextInputType.text,
    this.enableInteractiveSelection = true,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => _QrCodeCommonInputState();
}

class _QrCodeCommonInputState extends BaseState<QrCodeCommonInput> {
  late FocusNode _focusNode;
  late BoxDecoration boxDecoration;

  @override
  void initState() {
    super.initState();
    _focusNode = FocusNode();
    _pd();
    _focusNode.addListener(() {
      setState(() {
        _pd();
      });
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  _pd() {
    if (!_focusNode.hasFocus) {
      boxDecoration = BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: ColorsConfig.ffeeeeee,
            width: setWidth(1),
          ),
        ),
      );
    } else {
      boxDecoration = BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: ColorsConfig.primaryColor,
            width: setWidth(1),
          ),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final defaultTextSize = TodoLib.of(context).textSize;
    final defaultPlaceholderColor = TodoLib.of(context).placeholderColor;
    final defaultInputTextColor = TodoLib.of(context).inputTextColor;
    return CupertinoTextField(
      controller: widget.controller,
      focusNode: widget.focusNode ?? _focusNode,
      padding: widget.padding ?? symmetric(vertical: 10),
      placeholder: widget.placeholder,
      decoration: boxDecoration,
      style: TextStyle(
        color: widget.color ?? defaultInputTextColor,

        ///解决输入框提示与内容显示不一致问题
        height: 1.2,
        fontSize: setFontSize(widget.fontSize ?? defaultTextSize),
      ),
      placeholderStyle: TextStyle(
        color: widget.placeholderColor ?? defaultPlaceholderColor,
        fontSize: setFontSize(widget.placeholderFontSize ?? defaultTextSize),
      ),
      onChanged: (text) => widget.onTextChange?.call(text),
      cursorColor: Theme.of(context).textSelectionTheme.cursorColor,
      textAlign: widget.textAlign,
      textInputAction: widget.textInputAction,
      keyboardType: widget.keyboardType,
      inputFormatters: widget.inputFormatters,
      obscureText: widget.obscureText,
      obscuringCharacter: widget.obscuringCharacter,
      onSubmitted: widget.onSubmitted,
      maxLength: widget.maxLength,
      maxLines: widget.maxLines,
      autofocus: widget.autofocus,
      readOnly: widget.readOnly,
      prefix: widget.prefix,
      suffix: widget.suffix,
      prefixMode: widget.prefixMode,
      suffixMode: widget.suffixMode,
      enableInteractiveSelection: widget.enableInteractiveSelection,
    );
  }
}
